package com.xframe.domain.role;

import com.xframe.domain.BaseModel;
import com.xframe.xdal.core.annotation.*;
import com.xframe.xdal.core.constant.AutoFillType;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 用户角色关系表
 * @ClassName: SysUserRole
 * @Author: Lean.Liu
 * @CreateTime: 2023/4/3 17:27
 **/
@Data
@Accessors(chain=true)
@XTable(value = "sys_user_role", label = "用户角色关系表")
@XBelongsTo(typeName = "com.xframe.domain.role.SysRole",foreignKey = "roleId", targetKey = "roleId", as = "sysRole")
@XBelongsTo(typeName = "com.xframe.domain.role.SysUser",foreignKey = "userId", targetKey = "userId", as = "sysUser")
public class SysUserRole extends BaseModel {
    /**
     * 用户角色关系ID
     */
    @AutoFill(AutoFillType.Snowflake)
    @XColumn(name = "user_role_id", primaryKeyFlag = true)
    private Long userRoleId;

    /**
     * 用户ID
     */
    @XColumn(name = "user_id", uniqueFlag = true,desc="用户ID")
    private Long userId;

    /**
     * 角色ID
     */
    @XColumn(name = "role_id", uniqueFlag = true,desc="角色ID")
    private Long roleId;

    private SysUser sysUser;

    private SysRole sysRole;


    public static List<String> toIdArray(List<SysUserRole> userRoles){

        List<String> ids = new ArrayList<>();
        if(userRoles != null && userRoles.size() > 0){
            for(SysUserRole userRole : userRoles){
                ids.add(userRole.roleId.toString());
            }
        }
        return ids;
    }
}
